﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using TomanuExtensions;
using TomanuExtensions.Utils;

namespace AmiEmuLib.AmigaConfiguration.MemoryConfig
{
    /// <summary>
    /// Memory configuration.
    /// </summary>
    public class Memory : XmlBase
    {
        public MemorySize Z3MemorySize;
        public MemorySize FastMemorySize;
        public MemorySize SlowMemorySize;
        public MemorySize ChipMemorySize;
        public MemorySize MotherboardMemorySize;
        public MemorySize ProcessorSlotMemorySize;
        public Kickstart Kickstart;
        public KickstartExtended KickstartExtended;

        /// <summary>
        /// Should be valid for CDTV and CD32. Note they have diffrent memory type and size.
        /// </summary>
        public string FlashPath = "";

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="a_reader"></param>
        public Memory(XmlReader a_reader)
            : base(a_reader)
        {
        }

        public static readonly MemorySize[] ChipSizes = new MemorySize[] 
        { 
            Consts.MB_002, Consts.KB_1536, Consts.MB_001, Consts.KB_0512, Consts.KB_0256, (uint)0 
        };

        public static readonly MemorySize[] FastSizes = new MemorySize[] 
        { 
            Consts.MB_008, Consts.MB_004, Consts.MB_002, Consts.MB_001, (uint)0 
        };

        public static readonly MemorySize[] MotherboardMemory = new MemorySize[]
        { 
            Consts.MB_064, Consts.MB_032, Consts.MB_016, Consts.MB_008, 
            Consts.MB_004, Consts.MB_002, Consts.MB_001, (uint)0 
        };

        public static readonly MemorySize[] Z3Memory = new MemorySize[] 
        { 
            Consts.MB_512, Consts.MB_256, Consts.MB_128, Consts.MB_064, Consts.MB_032, 
            Consts.MB_016, Consts.MB_008, Consts.MB_004, Consts.MB_002, Consts.MB_001, (uint)0 
        };

        public static readonly MemorySize[] SlowSizes = new MemorySize[] 
        { 
            Consts.KB_1536, Consts.MB_001, Consts.KB_0512, Consts.KB_0256, (uint)0 
        };

        protected override void ReadXml(XmlReader a_reader)
        {
            a_reader.ReadStartElement(GetType().Name);

            Z3MemorySize = a_reader.ReadElementContentAsString("Z3MemorySize");
            FastMemorySize = a_reader.ReadElementContentAsString("FastMemorySize");
            SlowMemorySize = a_reader.ReadElementContentAsString("SlowMemorySize");
            ChipMemorySize = a_reader.ReadElementContentAsString("ChipMemorySize");
            MotherboardMemorySize = a_reader.ReadElementContentAsString("MotherboardMemorySize");
            ProcessorSlotMemorySize = a_reader.ReadElementContentAsString("ProcessorSlotMemorySize");

            Kickstart = new Kickstart(a_reader);
            KickstartExtended = new KickstartExtended(a_reader);

            FlashPath = a_reader.ReadElementContentAsString("FlashPath");

            a_reader.ReadEndElement();
        }

        public override void WriteXml(XmlWriter a_writer)
        {
            a_writer.WriteStartElement(GetType().Name);

            a_writer.WriteElement("Z3MemorySize", Z3MemorySize);
            a_writer.WriteElement("FastMemorySize", FastMemorySize);
            a_writer.WriteElement("SlowMemorySize", SlowMemorySize);
            a_writer.WriteElement("ChipMemorySize", ChipMemorySize);
            a_writer.WriteElement("MotherboardMemorySize", MotherboardMemorySize);
            a_writer.WriteElement("ProcessorSlotMemorySize", ProcessorSlotMemorySize);

            Kickstart.WriteXml(a_writer);
            KickstartExtended.WriteXml(a_writer);

            a_writer.WriteElement("FlashPath", FlashPath);

            a_writer.WriteEndElement();
        }
    }
}
